package com.food.eat.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author:RenPu
 * @Date: 2020/7/19 22:11
 * @Version: 1.0
 * @description: 自定义配置跨域规则
 */

@Configuration
public class CorsConfig {

    public CorsConfig() {
    }

    @Bean
    public CorsFilter getCorsConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("http://localhost:8081");  //设置允许请求后端的url；
        corsConfiguration.addAllowedMethod("*");                      //设置允许的方法
        corsConfiguration.addAllowedHeader("*");                      //设置允许的header
        corsConfiguration.setAllowCredentials(true);                  //设置是否返回cookie信息


        //为url添加映射路径
        UrlBasedCorsConfigurationSource corsSource = new UrlBasedCorsConfigurationSource();
        corsSource.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsFilter(corsSource);


    }

}
